<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="permissionList" row-key="id">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作" prop="name">
          <template #default="obj">
            <el-button type="text" @click="addPermission(obj.row.id,obj.row.type)">添加</el-button>
            <el-button type="text" @click="getIdPermission(obj.row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <!-- 表单对话框 -->
    <el-dialog :title="`新增权限点`" :visible="showPermissionForm" @close="cleanUpTheForm">
      <el-form ref="Form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="sure">确定</el-button>
          <el-button size="small" @click="cleanUpTheForm">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
// 引入权限类API
import { getPermissionAPI, addPermissionAPI, delPermissionAPI, getIdPermissionAPI, editPermissionAPI } from '@/api/permission'
// 其他API
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      permissionList: [], // 全部权限点数据
      showPermissionForm: false, // 权限表单显示
      // 表单数据
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      },
      // 表单校验规则
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 渲染页面数据
    async getPermission() {
      // 获取全部权限
      this.permissionList = await getPermissionAPI()
      this.permissionList = listToTreeData(this.permissionList, '0') // 转化为树形结构
    },
    // 显示添加表单
    addPermission(id, type) {
      this.form.pid = id
      this.form.type = type
      this.showPermissionForm = true
    },
    // 确认请求
    async sure() {
      // 校验表单
      await this.$refs.Form.validate()
      if (this.form.id !== undefined) {
        // 编辑请求
        await editPermissionAPI(this.form)
        this.$message.success('编辑成功')
      } else {
        // 添加请求
        await addPermissionAPI(this.form)
        this.$message.success('添加成功')
      }
      // 清理表单
      this.cleanUpTheForm()
      // 重新渲染
      this.getPermission()
    },
    // 清理表单
    cleanUpTheForm() {
      this.$refs.Form.resetFields()
      this.showPermissionForm = false
    },
    // 删除权限点
    async delPermission(id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delPermissionAPI(id)
      this.$message.success('删除成功')
      // 重新渲染
      this.getPermission()
    },
    // 显示编辑权限点表单并回显
    async getIdPermission(id) {
      this.form = await getIdPermissionAPI(id)
      this.showPermissionForm = true
    }
  }
}
</script>

<style>

</style>
